package com.bolt.report.business.modules.param.service;

import com.bolt.report.business.modules.param.dto.ReportDbParamDTO;
import com.bolt.report.business.modules.param.dto.converter.ReportDbParamConverter;
import com.bolt.report.business.modules.param.entity.ReportDbParamEntity;
import com.bolt.report.business.modules.param.repository.ReportDbParamRepository;
import com.bolt.support.base.service.BaseService;
import com.bolt.support.spring.jpa.jpql.Clauses;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-11-17 14:16:42
 **/
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
@RequiredArgsConstructor
public class ReportDbParamService extends BaseService<ReportDbParamEntity, String> {

    private final ReportDbParamRepository reportDbParamRepository;

    @Override
    protected BasicJpaRepository<ReportDbParamEntity, String> getRepository() {
        return reportDbParamRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(ReportDbParamDTO dto) {
        ReportDbParamEntity entity = ReportDbParamConverter.toEntity(dto);
        save(entity);
    }

    public Set<ReportDbParamDTO> findReportParam(String reportId) {
        List<ReportDbParamEntity> entities = this.jpaQueryFactory.from(ReportDbParamEntity.class)
                .where(k -> k.eq(Clauses.of(ReportDbParamEntity::getReportDbId), reportId))
                .fetch();
        return ReportDbParamConverter.toDTO(entities);
    }
}
